#include <iostream>
#include <cstring>
using namespace std;
const int N = 110;
int n, m;
int dp[N][N];
int points[N][N];
bool vis[N][N];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0 ,-1};
int ans = 0;
int dfs(int x, int y) {
    if (dp[x][y]) {
        return dp[x][y];
    }
    int dis = 1;
    for (int i = 0; i < 4; i++) {
        int x1 = x + dx[i];
        int y1 = y + dy[i];
        if (x1 >= 1 && x1 <= n && y1 >= 1 && y1 <= m && !vis[x1][y1] &&
                points[x1][y1] < points[x][y]) {
            vis[x1][y1] = true;
            dis = max(dis, dfs(x1, y1) + 1);
            vis[x1][y1] = false;
        }
    }
    dp[x][y] = dis;
    return dis;
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> points[i][j];
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            memset(vis, false, sizeof(vis));
            vis[i][j]=true;
            ans = max(ans, dfs(i, j));
        }
    }
    cout << ans << endl;
    return 0;
}